package com.example.clock_demo.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.TextStyle;
import java.util.Locale;

/**
 * 月度考勤报表Excel处理器 - 带说明列
 */
public class MonthlyAttendanceExcelHandler implements SheetWriteHandler {

    private final YearMonth yearMonth;

    public MonthlyAttendanceExcelHandler(YearMonth yearMonth) {
        this.yearMonth = yearMonth;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = writeSheetHolder.getSheet();

        // 创建样式
        CellStyle titleStyle = createTitleStyle(workbook);
        CellStyle headerStyle = createHeaderStyle(workbook);
        CellStyle dateHeaderStyle = createDateHeaderStyle(workbook);
        CellStyle descriptionStyle = createDescriptionStyle(workbook);

        // 创建带说明列的表头
        createHeaderWithDescription(sheet, titleStyle, headerStyle, dateHeaderStyle, descriptionStyle);
    }

    private void createHeaderWithDescription(Sheet sheet, CellStyle titleStyle, CellStyle headerStyle,
                                             CellStyle dateHeaderStyle, CellStyle descriptionStyle) {
        int daysInMonth = yearMonth.lengthOfMonth();

        // 第0行：标题
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(yearMonth.getYear() + "年" + yearMonth.getMonthValue() + "月考勤报表");
        titleCell.setCellStyle(titleStyle);

        // 计算总列数：4(固定列) + 1(说明列) + 31(日期列) + 10(汇总列) = 46列
        int totalColumns = 4 + 1 + 31 + 10;
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, totalColumns - 1));

        // 第1行：单行表头
        Row headerRow = sheet.createRow(1);

        // 固定列表头
        String[] fixedHeaders = {"序号", "姓名", "工号", "班组名称"};
        for (int i = 0; i < fixedHeaders.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(fixedHeaders[i]);
            cell.setCellStyle(headerStyle);
        }

        // 第5列：说明列表头
        Cell descriptionHeaderCell = headerRow.createCell(4);
        descriptionHeaderCell.setCellValue("日期星期");
        descriptionHeaderCell.setCellStyle(descriptionStyle);

        // 日期列表头 - 显示日期和星期
        for (int day = 1; day <= 31; day++) {
            Cell cell = headerRow.createCell(4 + day);
            if (day <= daysInMonth) {
                LocalDate date = yearMonth.atDay(day);
                String dayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.CHINESE);
                cell.setCellValue(day + "\n" + dayOfWeek);
            } else {
                cell.setCellValue("");
            }
            cell.setCellStyle(dateHeaderStyle);
        }

        // 汇总列表头
        String[] summaryHeaders = {"白班天数", "夜班天数", "排班工时", "出勤工时", "排班天数", "出勤天数", "缺勤天数", "迟到早退次数", "出勤工时比率", "出勤天数比率"};
        for (int i = 0; i < summaryHeaders.length; i++) {
            Cell cell = headerRow.createCell(5 + 31 + i);
            cell.setCellValue(summaryHeaders[i]);
            cell.setCellStyle(headerStyle);
        }

        // 设置行高
        titleRow.setHeightInPoints(30);
        headerRow.setHeightInPoints(40);

        // 设置列宽
        setColumnWidths(sheet);
    }

    private void setColumnWidths(Sheet sheet) {
        sheet.setColumnWidth(0, 8 * 256);     // 序号
        sheet.setColumnWidth(1, 12 * 256);    // 姓名
        sheet.setColumnWidth(2, 15 * 256);    // 工号
        sheet.setColumnWidth(3, 15 * 256);    // 班组名称
        sheet.setColumnWidth(4, 12 * 256);    // 说明列

        // 日期列宽度
        for (int i = 5; i < 5 + 31; i++) {
            sheet.setColumnWidth(i, 10 * 256);
        }

        // 汇总列宽度
        for (int i = 5 + 31; i < 5 + 31 + 10; i++) {
            sheet.setColumnWidth(i, 12 * 256);
        }
    }

    private CellStyle createTitleStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 14);
        style.setFont(font);

        return style;
    }

    private CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 11);
        style.setFont(font);

        return style;
    }

    private CellStyle createDateHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setWrapText(true);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        return style;
    }

    private CellStyle createDescriptionStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        return style;
    }
}
